const express = require('express')
const mysql = require('mysql')
const database = require('../database')
// 实例化服务器对象
const goods = express.Router()
goods.use(express.urlencoded({ extended: false }))
goods.use(express.json())
const db = mysql.createPool(database)
//2-4
goods.post('/classmate/findone', (req, res) => {
    let sql = 'select * from t_classmate where id=?'
    db.query(sql, [req.body.id], (err, result) => {
        if (result.length) {
            res.send({
                msg: '成功',
                success: true,
                data: result[0]
            })
        } else {
            res.send(err)
        }
    })
})

// 2-5
goods.post('/news/list', (req, res) => {
    let sql = 'select * from t_news'
    db.query(sql, [], (err, list) => {
        if (err) {
            res.send({
                success: false,
                msg: '失败'
            })
        } else {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        }
    })
})

//2-6
goods.post('/news/findone', (req, res) => {
    let sql = 'select * from t_news where id=?'
    db.query(sql, [req.body.id], (err, result) => {
        if (result.length) {
            res.send({
                success: true,
                msg: '成功',
                data: result[0]
            })
        } else {
            res.send(err)
        }
    })
})

module.exports = goods